<?php
/**
 * Created by dutuwang.net
 * User: adophper <hello@adophper.com>
 * Date: 2018/4/17
 * Time: 12:01
 */

?>
<script type="text/template" id="compare_tpl">
    <div class="comparepop comparepop-show">
        <div class="sidebar-content-iframe">
            <div class="comparepop-content">
                <ul class="comparepop-list">
                    <% if (obj != ''){ %>
                    <% _.each(obj, function(name, i){ %>
                    <li data-key="<%=i%>">
                        <a target="_blank" href="<?=Yii::$app->urlManager->createUrl(['zhanhui/detail', 'from' => 'compare'])?>&id=<%=i%>"><%=name%></a><i class="compare_remove glyphicon glyphicon-remove"></i>
                    </li>
                    <%
                    })
                    }else{
                    %>
                    <li>未添加数据</li>
                    <%
                    }
                    %>
                </ul>
                <div id="divCompare" class="comparepop-select" style="">
                    <div class="area comparepop-select-btn">
                        <a target="_blank" class="btn btn-orange pull-left j-compare-begin <%=obj.length==0?'btn-disabled':''%>" href="<?=Yii::$app->urlManager->createUrl(['zhanhui/compare'])?>">开始对比</a>
                        <div class="comparepop-alert">最多3个</div>
                        <a class="pull-right j-compare-clear <%=obj.length==0?'disabled':''%>" href="javascript:;">清空</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>
<div class="sidebar-wrap sidebar-mini-show sidebar-tool-show" id="sidebarWrap" data-page="compare">
    <div class="sidebar-mini" id="sidebarMiniBtn">展会对比<i class="glyphicon glyphicon-menu-left"></i></div>
    <div class="sidebar-content">
        <div class="sidebar-section">
            <div class="sidebar-tool-title">
                <a class="tool-tit-btn" id="hideContentBtn" href="javascript:void(0);" target="_self"><i class="glyphicon glyphicon-menu-right"></i></a>
                <div class="title" id="sidebarIframeTitle">展会对比</div>
            </div>
            <div class="sidebar-content-iframe" id="sidebarIframe"></div>
        </div>
    </div>
</div>
<script>
var set_url = '<?=Yii::$app->urlManager->createUrl(['zhanhui/compare-data'])?>';
var remove_url = '<?=Yii::$app->urlManager->createUrl(['zhanhui/remove-compare'])?>';
$(function () {
    //显示对比栏
    $("#sidebarMiniBtn").on('click', function (e) {
        e.preventDefault();
        $("#sidebarWrap").removeClass('sidebar-tool-show');
        $("#sidebarWrap").addClass('sidebar-content-show');
    })
    //隐藏对比栏
    $("#hideContentBtn").on('click', function(e){
        e.preventDefault();
        $("#sidebarWrap").removeClass('sidebar-content-show');
        $("#sidebarWrap").addClass('sidebar-tool-show');
    });
    //开始比较
    $(document).on('click', ".j-compare-begin", function (e) {
        if ($(this).hasClass('btn-disabled')) {
            e.preventDefault();
            return false;
        }
    })
    //清空
    $(document).on('click', ".j-compare-clear", function (e) {
        e.preventDefault();
        if ($(this).hasClass('disabled')) {
            return false;
        }
        removeCompare();
        $("input[id^=compare_]").attr('checked', false);
    })
    //添加
    $(document).on('click', ".compare_to", function (e) {
        var id = $(this).data('id');
        setCompare(id, 'add');
    })
    //删除
    $(document).on('click', ".compare_remove", function (e) {
        var id = $(this).parent().data('key');
        setCompare(id, 'remove');
        $("#compare_"+id).attr('checked', false);
    })
})
getCompare();
//获取对比数据
function getCompare(){
    $.get(set_url, {}, function (response) {
        if (response.code == 0) {
            var tmp = _.template($("#compare_tpl").html());
            $("#sidebarIframe").html(tmp(response.data));
        }
    }, 'json')
}
//设置对比数据
function setCompare(id, type){
    $.ajax({
        url: set_url,
        data: {id: id},
        dataType: 'json',
        type: 'POST',
        async: false,
        success: function (response) {
            if (response.code == 0) {
                var tmp = _.template($("#compare_tpl").html());
                $("#sidebarIframe").html(tmp(response.data));
            } else {
                alert(response.msg);
                if (type == 'add') {
                    $("#compare_"+id).attr('checked', false);
                }
            }
        },
        error: function (error) {
            alert(JSON.stringify(error));
        }
    })
}
//清除对比数据
function removeCompare(){
    $.get(remove_url, {}, function (response) {
        if (response.code == 0) {
            var tmp = _.template($("#compare_tpl").html());
            $("#sidebarIframe").html(tmp(response.data));
        }
    }, 'json');
}
</script>